<template>
  <div>
    <el-card class="box-card">
      <!-- card body -->
      <el-form
        :model="article"
        ref="form"
        :rules="rules"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="作者" prop="author">
              <el-input v-model="article.author"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标题">
              <el-input v-model="article.title"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="标题">
              <el-switch
                v-model="article.isShow"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="摘要">
              <el-input
                type="textarea"
                :rows="3"
                placeholder="请输入内容"
                v-model="article.summary"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="上传图片">
              <el-upload
                ref="file"
                class="upload-demo"
                action="/lejuAdmin/material/uploadFileOss"
                :on-success="fileSuccess"
                :headers="{ token: fileToken }"
              >
                <img
                  v-if="article.coverImg"
                  class="img"
                  :src="article.coverImg"
                  alt=""
                />
                <el-button v-else size="small" type="primary"
                  >点击上传</el-button
                >

                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="切换富文本类型">
              <el-radio v-model="radio" label="1" @change="changeRadioValue"
                >富文本</el-radio
              >
              <el-radio disabled v-model="radio" label="2" @change="changeRadioValue"
                >MaskDown</el-radio
              >
              <i class="el-icon-warning" style="color: #f00">
                注意!切换编辑器会清空编辑内容
              </i>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="富文本">
          <Tinymce v-model="article.content1" />
        </el-form-item>

        <el-form-item class="button">
          <el-button v-if="id" type="primary" @click="onSubmit">修改</el-button>
          <el-button v-else type="primary" @click="onSubmit">创建</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <buttomBanner></buttomBanner>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth.js";
import Tinymce from "@/components/Tinymce/index.vue";
import {
  addArticle,
  productArticle,
  updateArticle,
} from "@/api/content/editArticle/editArticle.js";

import buttomBanner from "@/components/buttomBanner";
export default {
  name: "ContentEditArticle",

  data() {
    return {
      name: "",
      id: "",
      radio: "1",
      article: {
        isShow: 1,
        summary: "",
        author: "",
        title: "",
        coverImg: "",
        content1: "",
      },
      rules: {
        author: [
          { required: true, message: "请输入作者名字", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    Tinymce,
    buttomBanner
  },
  computed: {
    // 上传图片带token值
    fileToken() {
      return getToken();
    },
  },
  methods: {
    init() {},
    // 图片上传成功后展示图片
    fileSuccess(file) {
      console.log(file);
      if (file.success) {
        this.article.coverImg = file.data.fileUrl;
        this.$refs.file.clearFiles();
      }
    },
    changeRadioValue(v) {
      console.log(v);
    },
    // 提交按钮
    onSubmit() {
      var api;
      if (this.id) {
        api = updateArticle;
        this.article.id = this.id;
      } else {
        api = addArticle;
      }

      this.article.content2 = this.article.content1;
      this.$refs.form.validate((valid) => {
        if (valid) {
          api(this.article).then((res) => {
            if (res.success) {
              if (this.id) {
                this.$message.success("修改成功");
              } else {
                this.$message.success("新增成功");
              }
              this.$router.go(-1);
            } else {
              this.$message.error(res.message);
            }
          });
        }
      });
    },
    // 取消按钮返回上一级
    goBack() {
      this.$router.go(-1);
    },
  },
  created() {
    this.id = this.$route.query.id;
    // 判断是新增还是修改
    if (this.id) {
      productArticle(this.id).then((res) => {
        if (res.success) {
          this.article = res.data.productArticle;
        } else {
          this.$message.error(message);
        }
      });
    }
  },
};
</script>

<style scoped lang="scss">
.img {
  width: 100px;
  height: 100px;
}
.button {
  text-align: center;
}
</style>